"""
    Partner Gate application 
    saves MD5 urls into datastore
    checks if MD5 url has already been
    accessed within a 7 day period
"""

from flask import Flask
from admin import admin
from url import pgate
from converters import md5Converter
from models import db


def create_app():
    app = Flask(__name__)
    app.config.from_pyfile('gate.cfg')
    db.init_app(app)

    # add converter for url matching for md5
    app.url_map.converters['md5'] = md5Converter

    # add 500/404 error handler
    app.error_handler_spec[None][500] = default_error_handler
    app.error_handler_spec[None][404] = page_not_found

    # register the blueprints
    app.register_blueprint(pgate) 
    app.register_blueprint(admin)
    return app    

def page_not_found(error):
    return "0", 200

def default_error_handler(error):
    return "0", 200

if __name__ == "__main__":
    app = create_app()
    app.run()